package com.ruoyi.web.wuchuan.util;


import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class PDFtest {
    public static PdfPTable createTable() throws DocumentException, IOException {
        PdfPTable table = new PdfPTable(4);
        PdfPCell cell;
        int size = 20;
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font font = new Font(bfChinese);
        font.setSize(20);
        font.setStyle(Font.BOLD);
        cell = new PdfPCell(new Phrase("坐标点生态报告", font));
        cell.setColspan(4);
        cell.setFixedHeight(30);//设置高度
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("坐标点基础信息", font));
        cell.setColspan(4);
        cell.setFixedHeight(30);//设置高度
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);//设置水平居中
        table.addCell(cell);

        Font font1 = new Font(bfChinese);
        font1.setStyle(Font.NORMAL);
        font1.setSize(10);
        PdfPCell cells = new PdfPCell(new Phrase("坐标点经度", font1));
        cells.setHorizontalAlignment(Element.ALIGN_LEFT);
        cells.setColspan(1);
        cells.setFixedHeight(size);//设置高度
        table.addCell(cells);
        cells = new PdfPCell(new Phrase("ceshi1", font1));
        table.addCell(cells);
        cells = new PdfPCell(new Phrase("坐标点纬度", font1));
        table.addCell(cells);
        cells = new PdfPCell(new Phrase("测试2", font1));
        table.addCell(cells);
        cells = new PdfPCell(new Phrase("所属行政区域", font1));
        table.addCell(cells);
         cells= new PdfPCell(new Phrase("测试3", font1));
        cells.setColspan(3);
        table.addCell(cells);
        return table;
    }

    public static void main(String[] args) throws Exception {

        PDFtest pdf = new PDFtest();
        String filename = "E:/test/a.pdf";
        pdf.createPDF(filename);
        System.out.println("打印完成");

    }

    public void createPDF(String filename) throws IOException {
        Document document = new Document(PageSize.A4);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
            document.addTitle("example of PDF");
            document.open();
            PdfPTable table = createTable();
            document.add(table);
        } catch (FileNotFoundException | DocumentException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
}
